package repository

import (
	"strconv"
	"yak.admin/src/entity"
	"yak.admin/src/service"
	"yak.admin/src/util"
)

type UserRepository struct {
}

func (repository *UserRepository) StatusLabels() map[int]string {
	return map[int]string {
		util.UserStatusReady: "审核中",
		util.UserStatusOK:    "通过",
	}
}

func (repository *UserRepository) Messages(userId uint, page int, pagesize int) []map[string]interface{} {
	table := "message_" + strconv.Itoa(int(userId % 10))
	reader := new(service.ReaderQuery).CreateQuery(table).
		WithEQ("user_id", userId).
		WithOrder("id", "desc").
		WithPage(page).WithPageSize(pagesize)

	var messages []*entity.Message
	reader.Reader(&messages)
	kfPhone := util.GetSettingValue("kf_phone")
	return reader.Datalist(messages, len(messages), func(index int, item interface{}) map[string]interface{} {
		m := item.(*entity.Message)
		return map[string]interface{}{
			"type": m.Type,
			"message": m.Message,
			"phone": kfPhone,
			"datetime": m.CreatedAt,
		}
	})
}
